package GameMain
{
	import Engine.Manager.ControlManager;
	import Engine.Manager.EngineManager;
	import Engine.Module.ModuleManager;
	
	import GameMain.Macro.Macro;
	import GameMain.Macro.MacroEvent;
	import GameMain.Model.EntityManager;
	import GameMain.Model.GlobleEventHandle;
	import GameMain.Model.GuiManager;
	import GameMain.Model.OwnPlayer;
	import GameMain.Model.Player;
	import GameMain.Model.SoundManager;
	import GameMain.View.Chat.Chat;
	import GameMain.View.Debug.Debug;
	import GameMain.View.FuBen.FuBen;
	import GameMain.View.GameErea.GameErea;
	import GameMain.View.GameOver.GameOver;
	import GameMain.View.Loading.Loading;
	import GameMain.View.Login.Login;
	import GameMain.View.MsgBox.MsgBox;
	import GameMain.View.NewPlayerTeach.NewPlayerTeach;
	import GameMain.View.PTGame.GameManager;
	import GameMain.View.PlayerBag.PlayerBag;
	import GameMain.View.Room.Room;
	import GameMain.View.RoomList.RoomList;
	import GameMain.View.ToolBar.ToolBar;
	import GameMain.View.WaitGame.WaitGame;
	
	import Interface.Common.IControlManager;
	import Interface.Common.IEngineManager;
	import Interface.Common.IEntityManager;
	import Interface.Common.IGuiManager;
	
	import flash.display.LoaderInfo;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.system.ApplicationDomain;
	
	import library.SocketData.SocketData;
	
	public class Game extends Sprite
	{
		private static var _engineMgr:EngineManager = null;
		private static var _controlMgr:ControlManager = null;
		private static var _moduleMgr:GuiManager = null;
		private static var _entityMgr:EntityManager = null;
		private static var _msgBox:MsgBox = null;
		private static var _globleEventhl:GlobleEventHandle = null;
		private static var _soundMgr:SoundManager = null;
		private static var _loadingUI:Loading = null;
		private static var _newPlayerTH:NewPlayerTeach = null;
		
		private var _loadSwfList:Array;
		private var _loadLength:int = 0;
		
		public function Game()
		{
			super();
			
			this._loadSwfList = [Macro.MODULE_GAME_EREA,Macro.MODULE_ROOM,Macro.MODULE_ROOM_LIST,Macro.MODULE_PT_GAME,Macro.MODULE_DEBUG,
				Macro.MODULE_CHAT,Macro.MODULE_TOOL_BAR,Macro.MODULE_LOGIN,Macro.MODULE_PLAYER_BAG,Macro.MODULE_MSG_BOX,Macro.MODULE_WAIT_GAME,Macro.MODULE_SOUND,Macro.MODULE_FUBEN,Macro.MODULE_NEW_PLAYER,
					Macro.MODULE_GAME_OVER];
			//this._loadSwfList = [Macro.MODULE_DEBUG];
			this.initGame();
			this.loadVersion();
			this.initEvent();
		}
		
		public static function get loadingUI():Loading
		{
			return _loadingUI;
		}
		
		public static function get EngineMgr():IEngineManager
		{
			return _engineMgr;
		}
		
		public static function get ControlMgr():IControlManager
		{
			return _controlMgr;
		}
		
		public static function get ModuleMgr():IGuiManager
		{
			return _moduleMgr;
		}
		
		public static function get newPlayerTH():NewPlayerTeach
		{
			return _newPlayerTH;
		}
		
		public static function get EntityMgr():IEntityManager
		{
			return _entityMgr;
		}
		
		public static function get ownPlayer():OwnPlayer
		{
			return _entityMgr.ownPlayer; 
		}
		
		public static function get soundMgr():SoundManager
		{
			return _soundMgr;
		}
		
		public static function ShowMsg(msgTxt:String,backFun:Function = null,state:int = 1):void
		{
			_msgBox.ShowMsg(msgTxt,backFun,state);
			Game._moduleMgr.showModuleFromIMD(_msgBox,ModuleManager.DEPTH_TOP);
		}
		
		private function initGame():void
		{
			Game._engineMgr = new EngineManager;
			Game._controlMgr = new ControlManager;
			Game._moduleMgr = new GuiManager;
			Game._moduleMgr.addToParent(this);
			Game._engineMgr.addLogDebug(this);
			Game._soundMgr = new SoundManager;
			Game._entityMgr = new EntityManager();
			Game._globleEventhl = new GlobleEventHandle();
		}
		
		private function initEvent():void
		{
			Game._engineMgr.registerEvent(MacroEvent.EVENT_DEBUG,showDebugWND);
			Game._engineMgr.registerEvent(MacroEvent.MSG_FROM_SERVER_LOG_SUCCESS,onPlayerLogSuccess);
			Game._engineMgr.registerEvent(MacroEvent.MSG_FROM_SERVER_LOG_FAIL,onPlayerLogFail);
		}
		
		private function onPlayerLogSuccess(data:SocketData):void
		{
			Game._engineMgr.destroyEvent(MacroEvent.MSG_FROM_SERVER_LOG_SUCCESS);
			Game._engineMgr.destroyEvent(MacroEvent.MSG_FROM_SERVER_LOG_FAIL);
			Game._entityMgr.createOwnPlayer(data);
			Game._moduleMgr.showGameErea();
		}
		
		private function onPlayerLogFail(data:SocketData):void
		{
			Game.ShowMsg("登陆失败,请重新登陆");
		}
		
		private function showDebugWND():void
		{
			Game.ModuleMgr.showBebug();
		}
		
		private function loadVersion():void
		{
			Game.EngineMgr.loadURLResVersion(null,initLoadingUI);
		}
		
		private function initLoadingUI():void
		{
			Game.EngineMgr.loadSWF(Macro.MODULE_LOADING,initLoadingOver);
			Game.EngineMgr.startloading();
		}
		
		private function initLoadingOver(e:Event,path:String):void
		{
			var info:LoaderInfo = e.currentTarget as LoaderInfo;
			_loadingUI = new Loading(info.content as Sprite);
			Game._moduleMgr.showModuleFromIMD(_loadingUI);
			this.startGame();
		}
		
		private function startGame():void
		{
			for(var i:int = 0;i<this._loadSwfList.length;i++)
			{
				Game.EngineMgr.loadSWF(this._loadSwfList[i],onLoadOver);
			}
			Game.EngineMgr.startloading();
		}
		
		private function onLoadOver(e:Event,path:String):void
		{
			this._loadLength++;
			var info:LoaderInfo = e.currentTarget as LoaderInfo;
			_loadingUI.setLoadingInfo((this._loadLength/this._loadSwfList.length)*100);
			switch(path)
			{
				case Macro.MODULE_GAME_EREA:
					Game.ModuleMgr.insertModule(path,new GameErea(info.content as Sprite));
					break;
				case Macro.MODULE_ROOM_LIST:
					Game.ModuleMgr.insertModule(path,new RoomList(info.content as Sprite,info));
					break;
				case Macro.MODULE_ROOM:
					Game.ModuleMgr.insertModule(path,new Room(info.content as Sprite));
					break;
				case Macro.MODULE_PT_GAME:
					GameManager.Init(info);
					break;
				case Macro.MODULE_DEBUG:
					Game.ModuleMgr.insertModule(path,new Debug(info.content as Sprite));
					break;
				case Macro.MODULE_CHAT:
					Game.ModuleMgr.insertModule(path,new Chat(info.content as Sprite));
					break;
				case Macro.MODULE_TOOL_BAR:
					Game.ModuleMgr.insertModule(path,new ToolBar(info.content as Sprite));
					break;
				case Macro.MODULE_LOGIN:
					Game.ModuleMgr.insertModule(path,new Login(info.content as Sprite));
					break;
				case Macro.MODULE_PLAYER_BAG:
					Game.ModuleMgr.insertModule(path,new PlayerBag(info.content as Sprite,info));
					break;
				case Macro.MODULE_MSG_BOX:
					_msgBox = new MsgBox(info.content as Sprite);
					break;
				case Macro.MODULE_WAIT_GAME:
					Game.ModuleMgr.insertModule(path,new WaitGame(info.content as Sprite));
					break;
				case Macro.MODULE_SOUND:
					Game._soundMgr.initSoundMgr(info);
					break;
				case Macro.MODULE_FUBEN:
					Game.ModuleMgr.insertModule(path,new FuBen(info.content as Sprite));
					break;
				case Macro.MODULE_NEW_PLAYER:
					_newPlayerTH = new NewPlayerTeach(info);
					break;
				case Macro.MODULE_GAME_OVER:
					Game.ModuleMgr.insertModule(path,new GameOver(info.content as Sprite));
					break;
				default:
					break;
			}
			if(this._loadLength == this._loadSwfList.length)
			{
				
				_loadingUI.dispose();
				//Game.ModuleMgr.showGameWait();
				//Game.ModuleMgr.showRoom();
				//Game.ModuleMgr.showGameErea();
				//Game.ModuleMgr.showPVPGame();
				//Game.ModuleMgr.showBebug();
				Game.ModuleMgr.showLogin();
				//Game.ModuleMgr.showRoomList();
				//(Game.ModuleMgr.getModule(Macro.MODULE_WAIT_GAME) as WaitGame).onLoadIngGame(1);
				//Game.ModuleMgr.showModule(Macro.MODULE_GAME_OVER);
			}
		}
		
	}
	
}